package com.myqm.util.gateway;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class ApiGatewayNetOp {
public static final String DEF_CHATSET = "UTF-8";
	
	//将map型转为请求参数型
    public static String urlencode(Map<String,Object>data) {
    	if (data == null)
    		return "";
        StringBuilder sb = new StringBuilder();
        for (Map.Entry i : data.entrySet()) {
            try {
                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
	/**
    *
    * @param strUrl 请求地址
    * @param params 请求参数
    * @param method 请求方法
    * @return  网络请求字符串
    * @throws Exception
    */
    public static String netByGET(String strUrl, Map params) throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            StringBuffer sb = new StringBuffer();
            
         	   if (params != null)
                strUrl = strUrl+"?"+urlencode(params); 
           
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json"); 
            
           
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);  
            conn.connect();
            
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }
    
    
    
   public static String netNotByGET(String strUrl, Object params,String method) throws Exception {
       HttpURLConnection conn = null;
       BufferedReader reader = null;
       String rs = null;
       try {
           StringBuffer sb = new StringBuffer(); 
           URL url = new URL(strUrl);
           conn = (HttpURLConnection) url.openConnection();
           conn.setConnectTimeout(30000);
           conn.setReadTimeout(30000);  
           conn.setRequestMethod(method);
           conn.setRequestProperty("Content-Type", "application/json"); 
           conn.setDoOutput(true);
           if(params != null) {
        	   String json = JSONObject.toJSONString(params); 
        	   byte[] bypes = json.getBytes();
        	   conn.getOutputStream().write(bypes);// 输入参数
           }
           
         //  conn.connect();
           conn.getOutputStream().flush();
           conn.getOutputStream().close();
           int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法
           if (responseCode / 200 == 1) {
           InputStream is = conn.getInputStream();
           reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
           String strRead = null;
           while ((strRead = reader.readLine()) != null) {
               sb.append(strRead);
           }
           rs = sb.toString();
           }
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           if (reader != null) {
               reader.close();
           }
           if (conn != null) {
               conn.disconnect();
           }
       }
       return rs;
   }
}
